#include "sessiondetailwidget.h"
#include <QVBoxLayout>
#include <QFontMetrics>
#include <QMessageBox>

#include "debug.h"
#include "choosefrienddialog.h"

////////////////////////////////////////////
/// 表示一个头像 + 一个名字组合控件
////////////////////////////////////////////

AvatarItem::AvatarItem(const QIcon &avatar, const QString &name)
{
    // 1. 设置自身的基本属性
    this->setFixedSize(60, 64);

    // 2. 创建布局管理器
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setAlignment(Qt::AlignHCenter);
    this->setLayout(layout);

    // 3. 创建头像
    QHBoxLayout* hLayout = new QHBoxLayout();
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(36, 36);
    avatarBtn->setIconSize(QSize(36, 36));
    avatarBtn->setIcon(avatar);
    avatarBtn->setStyleSheet("QPushButton { border: 1px solid rgb(218, 218, 218); }");
    hLayout->addWidget(avatarBtn);


    // 4. 创建名字
    nameLabel = new QLabel();
    nameLabel->setText(name);
    QFont font("微软雅黑", 9);
    nameLabel->setFont(font);
    nameLabel->setAlignment(Qt::AlignCenter);


    // 5. 名字 "截断"
    const int MAX_WIDTH = 55;
    QFontMetrics metrics(font);
    int totalWidth = metrics.horizontalAdvance(name);
    if (totalWidth >= MAX_WIDTH)
    {
        // 需要截断
        QString tail = "...";
        int tailWidth = metrics.horizontalAdvance(tail);
        int availableWidth = MAX_WIDTH - tailWidth;
        int availableSize = name.size() * ((double)availableWidth / totalWidth);
        QString newName = name.left(availableSize);
        nameLabel->setText(newName + tail);
    }

    layout->addLayout(hLayout);
    layout->addWidget(nameLabel);
}

////////////////////////////////////////////
/// 表示 "单聊会话详情" 窗口
////////////////////////////////////////////

SessionDetailWidget::SessionDetailWidget(QWidget *parent, const UserInfo& userInfo)
    : QDialog(parent)
    , userInfo(userInfo)
{
    // 1. 设置基本属性
    this->setWindowTitle("会话详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setFixedSize(280, 320);
    this->setStyleSheet("QWidget { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(40, 0, 50, 0);
    this->setLayout(layout);

    // 3. 添加 "创建群聊" 按钮
    // AvatarItem* currentUser = new AvatarItem(QIcon(":/resource/image/Mitsy.jpg"), "野原美伢");
    // layout->addWidget(currentUser, 0, 0);

    AvatarItem* createGroupBtn = new AvatarItem(QIcon(":/resource/image/cross.png"), "添加");
    layout->addWidget(createGroupBtn, 0, 1);

    // 4. 添加当前用户信息
    AvatarItem* currentUser = new AvatarItem(userInfo.avatar, userInfo.nickname);
    layout->addWidget(currentUser, 0, 0);

    // 5. "删除好友"
    deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setFixedSize(200, 30);
    deleteFriendBtn->setText("删除好友");
    QString style = "QPushButton { border: 1px solid rgb(205, 205, 205); border-radius: 5px; "
                    "color: red; }";
    style += "QPushButton::pressed { color: rgb(251, 165, 151);}";
    deleteFriendBtn->setStyleSheet(style);
    layout->addWidget(deleteFriendBtn, 1, 0, 1, 3);

    // 6. 添加信号槽
    connect(createGroupBtn->getAvatar(), &QPushButton::clicked, this, [=](){
        ChooseFriendDialog* chooseFriendDialog = new ChooseFriendDialog(this, userInfo.userId);
        chooseFriendDialog->exec();
    });

    connect(deleteFriendBtn, &QPushButton::clicked, this, &SessionDetailWidget::clickDeleteFriendBtn);
}

void SessionDetailWidget::clickDeleteFriendBtn()
{
    // 1. 确认是否删除
    auto result = QMessageBox::warning(this, "删除好友", "确定删除该好友吗?", QMessageBox::Ok | QMessageBox::Cancel);
    if (result != QMessageBox::Ok)
    {
        LOG("用户取消了好友删除");
        return ;
    }

    // 2. 发送好友删除的请求
    DataCenter* dataCenter = DataCenter::getInstance();
    dataCenter->deleteFriendAsync(this->userInfo.userId);

    // 3. 关闭窗口
    this->close();
}



















